package com.apes.fn.tms.service;

import com.apes.fn.tms.model.MainShift;
import com.apes.fn.tms.repository.MainShiftRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("mainShiftService")
public class MainShiftService extends DomainService {
    @Autowired
    MainShiftRepository mainShiftRepository;

    public MainShift create(SimpleRequest simpleRequest){
        MainShift mainShift = simpleRequest.getO(MainShift.class);
        return mainShiftRepository.saveAndFlush(mainShift);
    }

    public MainShift update(SimpleRequest simpleRequest){
        MainShift mainShift = simpleRequest.getO(MainShift.class);
        return mainShiftRepository.saveAndFlush(mainShift);
    }

    private String delete(SimpleRequest simpleRequest){
        Integer versions = simpleRequest.get("version");
        MainShift mainShift = simpleRequest.getO(MainShift.class);
        if (mainShift.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        mainShiftRepository.delete(mainShift);
        return "删除成功";
    }

}
